<template>
  <div class="page-container" v-global-loading="loading">
    <div class="page-card-container">
      <a-form :model="pageSet" layout="inline">
        <a-form-item label="订单编号">
          <a-input
            v-model:value="pageSet.codeSN"
            style="width: 170px"
            :allowClear="true"
          ></a-input>
        </a-form-item>
        <a-form-item label="收货人">
          <a-input
            v-model:value="pageSet.consignee"
            style="width: 170px"
            :allowClear="true"
          ></a-input>
        </a-form-item>
        <a-form-item label="下单时间">
          <a-range-picker
            v-model:value="pageSet.applyStatuDate"
            value-format="YYYY-MM-DD"
            format="YYYY-MM-DD"
            @change="changePicker"
          />
        </a-form-item>
        <a-form-item label="">
          <div class="commonBtn-warning" @click="loadPagination(true)">
            查询
          </div>
        </a-form-item>
        <a-form-item label="">
          <div class="commonBtn-primary" @click="reset">重置</div>
        </a-form-item>
        <a-form-item label="">
          <div class="commonBtn-primary" @click="operation(1)">新建</div>
        </a-form-item>
      </a-form>
      <a-table
        size="small"
        :loading="tableLoading"
        :dataSource="tableData"
        :columns="columns"
        bordered
        :pagination="false"
        :scroll="{
          x: 1200,
          y: useAppStore.pageCommonInfo.canUseHeight - 36 + 'px'
        }"
      >
        <template #bodyCell="{text, record, index, column}"> </template>
      </a-table>
      <Pagination
        :pageSet="pageSet"
        style="margin-top: 10px"
        :loadPagination="loadPagination"
      ></Pagination>
      <a-modal
        v-model:open="visible"
        width="1000px"
        :title="null"
        :footer="null"
        :maskClosable="false"
        :closable="false"
      >
        <div v-global-loading="modalloading">
          <div style="margin: 10px 0px">
            <a-steps
              :current="current"
              :items="[
                {
                  title: '选择商品'
                },
                {
                  title: '提交订单'
                },
                {
                  title: '完成'
                }
              ]"
            ></a-steps>
          </div>
          <!-- 选择商品 -->
          <div v-if="current == 0">
            <a-form :labelCol="{span: 7}" :model="form" ref="formRef">
              <a-row :gutter="24">
                <a-col :span="12">
                  <a-form-item label="选择会员" name="membersId">
                    <a-select
                      v-model:value="form.membersId"
                      style="width: 100%"
                      :filter-option="filterOption"
                      show-search
                      @change="changeMemberList"
                    >
                      <a-select-option
                        :value="item.id"
                        :title="item.name"
                        v-for="(item, index) in memberList"
                      >
                        <span style="min-width: 60px; display: inline-block">{{
                          item.name
                        }}</span>
                        &nbsp;
                        <span>{{ item.phone }}</span>
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
                <a-col :span="12">
                  <a-form-item label="选择主体" name="bodyId">
                    <a-select
                      v-model:value="form.bodyId"
                      style="width: 100%"
                      :filter-option="filterOption"
                      show-search
                      @change="changeMemberList1"
                    >
                      <a-select-option
                        :value="item.id"
                        :title="item.positionName"
                        v-for="(item, index) in enterpriseBodyListData"
                        >{{ item.customerName }}
                      </a-select-option>
                    </a-select>
                  </a-form-item>
                </a-col>
              </a-row>
              <!-- <a-row :gutter="24">
              <a-col :span="8">
                <a-form-item label="选择会员" name="membersId">
                  <a-select v-model:value="form.membersId" style="width: 100%" :filter-option="filterOption" show-search
                    @change="changeMemberList">
                    <a-select-option :value="item.id" :title="item.name" v-for="(item, index) in memberList">
                      <span style="min-width: 60px; display: inline-block">{{
                        item.name
                      }}</span>
                      &nbsp;
                      <span>{{ item.phone }}</span>
                    </a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item label="客户订单号" name="customerOrderSN">
                  <a-input v-model:value="form.customerOrderSN" />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="选择主体" name="bodyId">
                  <a-select v-model:value="form.bodyId" style="width: 100%" :filter-option="filterOption" show-search
                    @change="changeMemberList1">
                    <a-select-option :value="item.id" :title="item.positionName"
                      v-for="(item, index) in enterpriseBodyListData">{{ item.customerName }}
                    </a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item label="客户备注" name="customerRemark">
                  <a-input v-model:value="form.customerRemark" />
                </a-form-item>
              </a-col>
              <a-col :span="8">
                <a-form-item label="支付方式" name="payWay">
                  <a-select v-model:value="form.payWay" style="width: 100%">
                    <a-select-option :value="1" title="支付宝">支付宝
                    </a-select-option>
                    <a-select-option :value="2" title="线下汇款">线下汇款
                    </a-select-option>
                    <a-select-option :value="3" title="授信支付">授信支付
                    </a-select-option>
                  </a-select>
                </a-form-item>
                <a-form-item label="附件" name="attachment">
                  <div><span>查看附件信息</span> <span class="pointer c-primaryColor">点击上传</span></div>
                </a-form-item>
              </a-col>
            </a-row> -->
            </a-form>
            <div style="font-size: 18px; font-weight: 800; margin-bottom: 10px">
              选择商品：<span
                class="pointer c-primaryColor"
                @click="selectGoods"
                >点击选择</span
              >
            </div>
            <!-- 选择商品 -->
            <a-modal
              v-model:open="visible1"
              width="1000px"
              title="选择商品"
              :footer="null"
              :maskClosable="false"
              :destroyOnClose="true"
            >
              <UserGradeChooseClone
                ref="UserGradeChooseCloneRef"
                v-model:list="chooseID"
                v-model:data="form.shopProductInfos"
                v-model:bodyId="form.bodyId"
                v-model:membersId="form.membersId"
              >
              </UserGradeChooseClone>
              <div style="display: flex; justify-content: end">
                <a-button type="primary" @click="visible1 = false"
                  >确定</a-button
                >
              </div>
            </a-modal>
            <!-- 设置商品规格 -->
            <a-table
              size="small"
              :loading="tableLoading1"
              :dataSource="form.shopProductInfos"
              :columns="columns1"
              bordered
              :pagination="false"
              :scroll="{x: 700, y: 500}"
            >
              <template #bodyCell="{text, record, index, column}">
                <template v-if="column.title == '商品数量'">
                  <a-input-number
                    :min="1"
                    style="width: 100%"
                    v-model:value="form.shopProductInfos[index].num"
                    @change="totalCommodity(index)"
                  ></a-input-number>
                </template>
                <template v-if="column.title == '折扣%'">
                  <a-input-number
                    :disabled="
                      !judgeBtnPermission(
                        'orderManagementList:orderProductUpdate'
                      )
                    "
                    :min="50"
                    :max="100"
                    style="width: 100%"
                    @change="totalCommodity(index)"
                    v-model:value="
                      form.shopProductInfos[index].surrogateDiscount
                    "
                  ></a-input-number>
                </template>
                <template v-if="column.title == '含税单价(元)'">
                  <div v-if="record.memberPrice == record.taxPrice">
                    <span>￥{{ text }}</span>
                  </div>
                  <div v-else>
                    <span style="text-decoration: line-through"
                      >￥{{ text }}</span
                    ><br />
                    <span>￥{{ record.memberPrice }}</span>
                  </div>
                </template>
                <template v-if="column.title == '操作'">
                  <span
                    style="color: red"
                    class="pointer"
                    @click="delCommodity(index)"
                    >删除</span
                  >
                </template>
              </template>
            </a-table>
            <!-- 选择收货地址 -->
            <div
              style="
                font-size: 18px;
                font-weight: 800;
                margin-top: 10px;
                display: flex;
                justify-content: space-between;
                padding-right: 10px;
              "
            >
              <div>请选择收货地址：</div>
              <div
                class="pointer c-primaryColor"
                @click="addressMessageOperator(1)"
              >
                新建
              </div>
            </div>
            <a-table
              size="small"
              :loading="tableLoading2"
              :dataSource="tableData2"
              :columns="columns2"
              bordered
              :pagination="false"
              row-key="id"
              :row-selection="{
                hideSelectAll: true,
                selectedRowKeys: form.receivingAddressId,
                onChange: (e) => {
                  onSelectChange(e, 1);
                }
              }"
              :scroll="{x: 700, y: 500}"
            >
              <template #bodyCell="{text, record, index, column}">
                <template v-if="column.title == '操作'">
                  <span
                    class="pointer"
                    @click="addressMessageOperator(1, record.id)"
                    >详情</span
                  >
                </template>
              </template>
            </a-table>
            <!-- 选择发票收货地址 -->
            <div
              style="
                font-size: 18px;
                font-weight: 800;
                margin-top: 10px;
                display: flex;
                justify-content: space-between;
                padding-right: 10px;
              "
            >
              <div>请选择收票地址：</div>
              <div
                class="pointer c-primaryColor"
                @click="addressMessageOperator(2)"
              >
                新建
              </div>
            </div>
            <a-table
              size="small"
              :loading="tableLoading3"
              :dataSource="tableData3"
              :columns="columns3"
              bordered
              :pagination="false"
              row-key="id"
              :row-selection="{
                hideSelectAll: true,
                selectedRowKeys: form.invoiceAddressId,
                onChange: (e) => {
                  onSelectChange(e, 2);
                }
              }"
              :scroll="{x: 700, y: 500}"
            >
              <template #bodyCell="{text, record, index, column}">
                <template v-if="column.title == '操作'">
                  <span
                    class="pointer"
                    @click="addressMessageOperator(2, record.id)"
                    >详情</span
                  >
                </template></template
              >
            </a-table>
            <!-- 选择发票信息 -->
            <div style="display: flex; line-height: 30px; margin-top: 10px">
              <div style="font-size: 18px; font-weight: 800">
                请选择发票类型：
              </div>
              <a-radio-group
                v-model:value="form.invoiceType"
                button-style="solid"
              >
                <a-radio-button
                  :value="item.type"
                  v-for="item in invoiceType"
                  >{{ item.name }}</a-radio-button
                >
              </a-radio-group>
            </div>
            <!-- <a-table size="small" :loading="tableLoading4" :dataSource="tableData4" :columns="columns4" bordered
              :pagination="false" row-key="id" :row-selection="{
                hideSelectAll: true,
                selectedRowKeys: form.invoiceId,
                onChange: (e) => {
                  onSelectChange(e, 3);
                }
              }" :scroll="{ x: 700, y: 500 }">
              <template #bodyCell="{ text, record, index, column }"> </template>
            </a-table> -->
            <div style="display: flex; justify-content: end; margin-top: 10px">
              <a-button @click="visible = false" style="margin-right: 10px"
                >取消</a-button
              >
              <a-button type="primary" @click="settleAccounts">结算</a-button>
            </div>
          </div>
          <!-- 验证信息 -->
          <div v-if="current == 1">
            <SettleAccounts
              ref="SettleAccountsRef"
              v-model:takeList="tableData2"
              v-model:ticketList="tableData3"
              v-model:submitData="form"
              v-model:data="finalStatement"
              @statement="statement"
              @pressUndo="pressUndo"
              @cancel="cancel"
            ></SettleAccounts>
            <!-- v-model:invoiceList="tableData4" -->
          </div>
          <!-- 提交成功 -->
          <div v-if="current == 2">
            <a-result
              status="success"
              title="订单提交成功!"
              sub-title="订单提交成功请通知客户进行核实。"
            >
              <template #extra>
                <a-button key="console" type="primary" @click="verify"
                  >确定</a-button
                >
              </template>
            </a-result>
          </div>
        </div>
      </a-modal>
    </div>
    <AddressMessage
      ref="AddressMessageRef"
      :bodyId="form.bodyId"
      :membersId="form.membersId"
      @ok="AddressMessageOK"
    >
    </AddressMessage>
  </div>
</template>
<script lang="ts" setup>
import {ref, inject, onActivated, onMounted, nextTick, watch} from "vue";
import {arithmetic} from "@/utils/common";
import {orderPage} from "@/api/orderManagement/orderManagementList";
import SettleAccounts from "./settleAccounts.vue";
import useAppStore from "@/store/app";
import AddressMessage from "./AddressMessage.vue";
import UserGradeChooseClone from "@/components/functional/userGradeChooseClone.vue";
import Pagination from "@/components/pagination/Pagination.vue";
import {membersInfoList} from "@/api/customerArchives/memberManagement";
import {
  enterpriseBodyList,
  receivingAddressList,
  invoiceInfoList,
  getOrderSettlementInfo,
  createMemberAddress
} from "@/api/orderManagement/behalfConsumer";
import {message} from "ant-design-vue";
import {judgeBtnPermission} from "@/utils/btnPermission";
const calcCanUseHeight: any = inject("calcCanUseHeight");
onActivated(() => {
  calcCanUseHeight();
});
onMounted(() => {
  calcCanUseHeight();
});
const AddressMessageRef = ref();
const addressMessageOperator = (type: number, id = 0) => {
  if (form.value.membersId && form.value.bodyId) {
    AddressMessageRef.value.add(type, id);
  } else {
    message.error("请先选择会员和主体信息!");
  }
};
//添加地址回调
const AddressMessageOK = (type) => {
  if (type == 1) {
    tableLoading2.value = true;
  } else {
    tableLoading3.value = true;
  }
  receivingAddressList({
    type,
    membersId: form.value.membersId,
    bodyId: form.value.bodyId
  })
    .then((res) => {
      if (type == 1) {
        tableData2.value = res.data;
        tableData2.value.forEach((item) => {
          if (item.isDefault) {
            form.value.receivingAddressId = [item.id];
          }
        });
      } else {
        tableData3.value = res.data;
        tableData3.value.forEach((item) => {
          if (item.isDefault) {
            form.value.invoiceAddressId = [item.id];
          }
        });
      }
    })
    .finally(() => {
      tableLoading2.value = false;
      tableLoading3.value = false;
    });
};
//操作
const operation = (type: number) => {
  if (type == 1) {
    //新建
    current.value = 0;
    chooseID.value = [];
    form.value = {shopProductInfos: []};
    // tableData4.value = [];
    tableData3.value = [];
    tableData2.value = [];
    visible.value = true;
  }
};
//当前操作的进度
const current = ref(0);
//会员集合
const memberList = ref<Array<any>>([]);
//主体信息
//选择商品信息ID集合
const chooseID = ref([]);
const enterpriseBodyListData = ref<Array<any>>([]);
const getMemberList = () => {
  membersInfoList().then((res) => {
    memberList.value = res.data;
  });
};
const changeMemberList = () => {
  enterpriseBodyListData.value = [];
  form.value.bodyId = null;
  form.value.invoiceAddressId = [];
  form.value.receivingAddressId = [];
  form.value.invoiceId = [];
  tableData2.value = [];
  tableData3.value = [];
  // tableData4.value = [];
  enterpriseBodyList({membersId: form.value.membersId, applyStatus: 9}).then(
    (res) => {
      enterpriseBodyListData.value = res.data;
    }
  );
};
const changeMemberList1 = () => {
  //判断主体ID和会员ID有值请求收货地址
  if (form.value.membersId && form.value.bodyId) {
    tableLoading2.value = true;
    tableLoading3.value = true;
    tableLoading4.value = true;
    receivingAddressList({
      type: 1,
      membersId: form.value.membersId,
      bodyId: form.value.bodyId
    })
      .then((res) => {
        tableData2.value = res.data;
        tableData2.value.forEach((item) => {
          if (item.isDefault) {
            form.value.receivingAddressId = [item.id];
          }
        });
      })
      .finally(() => {
        tableLoading2.value = false;
      });
    receivingAddressList({
      type: 2,
      membersId: form.value.membersId,
      bodyId: form.value.bodyId
    })
      .then((res) => {
        tableData3.value = res.data;
        tableData3.value.forEach((item) => {
          if (item.isDefault) {
            form.value.invoiceAddressId = [item.id];
          }
        });
      })
      .finally(() => {
        tableLoading3.value = false;
      });
    // invoiceInfoList({
    //   membersId: form.value.membersId,
    //   bodyId: form.value.bodyId
    // })
    //   .then((res) => {
    //     tableData4.value = res.data;
    //     tableData4.value.forEach((item) => {
    //       if (item.isDefault) {
    //         form.value.invoiceId = [item.id];
    //       }
    //     });
    //   })
    //   .finally(() => {
    //     tableLoading4.value = false;
    //   });
  }
};
getMemberList();
const form = ref<any>({shopProductInfos: []});
const formRef = ref();
const loading = ref(false);
const visible = ref(false);
const visible1 = ref(false);
const modalloading = ref(false);
const pageSet = ref<any>({
  pageNo: 1,
  pageSize: 20,
  total: 0,
  createChannel: 4
});
const tableData = ref<Array<any>>([]);
const tableData1 = ref<Array<any>>([]);
const tableData2 = ref<Array<any>>([]);
const tableData3 = ref<Array<any>>([]);
// const tableData4 = ref<Array<any>>([]);
const tableLoading = ref(false);
const tableLoading1 = ref(false);
const tableLoading2 = ref(false);
const tableLoading3 = ref(false);
const tableLoading4 = ref(false);
//获取数据
const loadPagination = (flag: boolean = false): void => {
  if (flag) {
    pageSet.value.pageNo = 1;
  }
  tableData.value = [];
  tableLoading.value = true;
  orderPage(pageSet.value)
    .then((res) => {
      tableData.value = res.data.rows;
      pageSet.value.total = res.data.totalRows;
    })
    .finally(() => {
      tableLoading.value = false;
    });
};
//重置
const reset = (): void => {
  pageSet.value = {
    pageNo: 1,
    pageSize: 20,
    total: 0,
    createChannel: 4
  };
  loadPagination(true);
};
function changePicker(a, b) {
  if (a) {
    pageSet.value.beginTime = b[0];
    pageSet.value.endTime = b[1];
  } else {
    pageSet.value.beginTime = null;
    pageSet.value.endTime = null;
  }
}
//订单状态
const orderStatus = ref([
  {label: "已提交", value: 0},
  {label: "待支付订单", value: 1},
  {label: "待审核订单", value: 2},
  {label: "待发货订单", value: 3},
  {label: "待收货订单", value: 4},
  {label: "待评价订单", value: 5},
  {label: "交易成功", value: 6},
  {label: "售后中", value: 7},
  {label: "售后完成", value: 8},
  {label: "售后关闭", value: 9},
  {label: "交易关闭", value: 10}
]);
const columns = ref<Array<object>>([
  {
    title: "序号",
    dataIndex: "name",
    align: "center",
    width: 50,
    customRender: ({index}) => {
      return index + 1;
    },
    fixed: "left"
  },
  {
    title: "订单编号",
    dataIndex: "codeSN",
    align: "center",
    width: 200
  },
  {
    title: "用户名",
    dataIndex: "memberName",
    align: "center",
    width: 120
  },
  {
    title: "实际支付(元)",
    dataIndex: "payAmount",
    align: "center",
    width: 120
  },
  {
    title: "支付方式",
    dataIndex: "payWay",
    align: "center",
    width: 120,
    customRender: ({value}) => {
      if (value == 1) {
        return "支付宝";
      } else if (value == 2) {
        return "线下汇款";
      } else if (value == 3) {
        return "授信支付";
      }
    }
  },
  {
    title: "支付订单编号",
    dataIndex: "payCodeSN",
    align: "center",
    width: 240
  },
  {
    title: "收货人名称",
    dataIndex: "consignee",
    align: "center",
    width: 160
  },
  {
    title: "订单状态",
    dataIndex: "orderStatus",
    align: "center",
    width: 180,
    customRender: ({value}) => {
      let name = "";
      orderStatus.value.forEach((item) => {
        if (value == item.value) {
          name = item.label;
        }
      });
      return name;
    }
  },
  // {
  //   title: "订单来源",
  //   dataIndex: "createChannel",
  //   align: "center",
  //   width: 120,
  //   customRender: ({ value }) => {
  //     if (value == 0) {
  //       return "客户端";
  //     } else if (value == 4) {
  //       return "代客下单";
  //     }
  //   }
  // },
  {
    title: "下单时间",
    dataIndex: "addTime",
    align: "center",
    width: 180
  }
]);
const columns1 = ref<Array<object>>([
  {
    title: "序号",
    dataIndex: "index",
    align: "center",
    width: 60,
    customRender: ({index}) => {
      return index + 1;
    }
  },
  {
    title: "商品名称",
    dataIndex: "productName",
    align: "center",
    width: 220
  },
  {
    title: "含税单价(元)",
    dataIndex: "taxPrice",
    align: "center",
    width: 100
  },
  // {
  //   title: "客户物料号",
  //   dataIndex: "materialNum",
  //   align: "center",
  //   width: 100
  // },
  // {
  //   title: "客户物料名称",
  //   dataIndex: "materialName",
  //   align: "center",
  //   width: 100
  // },
  // {
  //   title: "客户物料规格",
  //   dataIndex: "materialSpec",
  //   align: "center",
  //   width: 100
  // },
  // {
  //   title: "客户物料单位",
  //   dataIndex: "materialUnit",
  //   align: "center",
  //   width: 100
  // },
  // {
  //   title: "物料数量",
  //   dataIndex: "materialUnit",
  //   align: "center",
  //   width: 100
  // },
  {
    title: "商品数量",
    dataIndex: "num",
    align: "center",
    width: 100
  },
  {
    title: "折扣%",
    dataIndex: "surrogateDiscount",
    align: "center",
    width: 100
  },
  {
    title: "合计",
    dataIndex: "heji",
    align: "center",
    width: 100
  },
  {
    title: "操作",
    dataIndex: "caozuo",
    align: "center",
    width: 100,
    fixed: "right"
  }
]);
const columns2 = ref<Array<object>>([
  {
    title: "姓名",
    dataIndex: "name",
    align: "center",
    width: 120
  },
  {
    title: "电话",
    dataIndex: "phone",
    align: "center",
    width: 120
  },
  {
    title: "省",
    dataIndex: "province",
    align: "center",
    width: 80
  },
  {
    title: "市",
    dataIndex: "city",
    align: "center",
    width: 80
  },
  {
    title: "县",
    dataIndex: "county",
    align: "center",
    width: 80
  },
  {
    title: "详细地址",
    dataIndex: "address",
    align: "center",
    width: 150
  },
  {
    title: "操作",
    dataIndex: "caozuo",
    align: "center",
    width: 80
  }
]);
const columns3 = ref<Array<object>>([
  {
    title: "姓名",
    dataIndex: "name",
    align: "center",
    width: 120
  },
  {
    title: "电话",
    dataIndex: "phone",
    align: "center",
    width: 120
  },
  {
    title: "省",
    dataIndex: "province",
    align: "center",
    width: 80
  },
  {
    title: "市",
    dataIndex: "city",
    align: "center",
    width: 80
  },
  {
    title: "县",
    dataIndex: "county",
    align: "center",
    width: 80
  },
  {
    title: "详细地址",
    dataIndex: "address",
    align: "center",
    width: 150
  },
  {
    title: "操作",
    dataIndex: "caozuo",
    align: "center",
    width: 80
  }
]);
const invoiceType = ref([
  {name: "增值税专用票", type: "Z1"},
  {name: "增值税电子普通发票", type: "Z3"},
  {name: "数电增值税专票", type: "Z5"},
  {name: "数电增值税普票", type: "Z6"}
]);
const columns4 = ref<Array<object>>([
  {
    title: "企业名称",
    dataIndex: "companyName",
    align: "center",
    width: 120
  },
  {
    title: "发票类型",
    dataIndex: "invoiceType",
    align: "center",
    width: 120,
    customRender: ({value}) => {
      if (value == 1) {
        return "增值税专用票";
      } else if (value == 2) {
        return "增值税专用电子发票";
      } else if (value == 3) {
        return "电子普通发票";
      } else if (value == 4) {
        return "普通发票";
      }
    }
  },
  {
    title: "发票抬头",
    dataIndex: "invoiceTitle",
    align: "center",
    width: 120
  },
  {
    title: "注册地址",
    dataIndex: "address",
    align: "center",
    width: 120
  },
  {
    title: "开户行",
    dataIndex: "bank",
    align: "center",
    width: 120
  },
  {
    title: "银行账号",
    dataIndex: "account",
    align: "center",
    width: 120
  },
  {
    title: "发票备注",
    dataIndex: "remark",
    align: "center",
    width: 120
  }
]);
const filterOption = (input: string, option: any) => {
  return option.title.toLowerCase().indexOf(input.toLowerCase()) >= 0;
};
const onSelectChange = (e, type) => {
  if (type == 1) {
    if (e.length > 1) {
      form.value.receivingAddressId = [e[e.length - 1]];
    } else {
      form.value.receivingAddressId = e;
    }
  } else if (type == 2) {
    if (e.length > 1) {
      form.value.invoiceAddressId = [e[e.length - 1]];
    } else {
      form.value.invoiceAddressId = e;
    }
  } else if (type == 3) {
    if (e.length > 1) {
      form.value.invoiceId = [e[e.length - 1]];
    } else {
      form.value.invoiceId = e;
    }
  }
};
//删除已选择的商品
const delCommodity = (index) => {
  form.value.shopProductInfos.splice(index, 1);
  chooseID.value.splice(index, 1);
};
//计算商品合计
const totalCommodity = (index) => {
  let data = JSON.parse(JSON.stringify(form.value.shopProductInfos[index]));
  //单价
  if (data.memberPrice == data.taxPrice) {
    data.unitPrice = data.taxPrice;
  } else {
    data.unitPrice = data.memberPrice;
  }
  if (data.surrogateDiscount) {
    data.unitPrice = arithmetic(
      data.unitPrice1,
      data.surrogateDiscount / 100,
      "multiply"
    );
  }
  let heji = arithmetic(data.unitPrice, data.num, "multiply");
  form.value.shopProductInfos[index].heji = heji;
  form.value.shopProductInfos[index].memberPrice = data.unitPrice;
};
//结算
const finalStatement = ref({});
const settleAccounts = () => {
  if (!form.value.membersId) {
    message.error("请选择会员!");
    return false;
  }
  if (!form.value.bodyId) {
    message.error("请选择主体信息!");
    return false;
  }
  if (!form.value.shopProductInfos.length) {
    message.error("请选择商品!");
    return false;
  }
  if (!form.value.receivingAddressId.length) {
    message.error("请选择收货地址!");
    return false;
  }
  modalloading.value = true;
  let formData = JSON.parse(JSON.stringify(form.value));
  formData.receivingAddressId = formData.receivingAddressId.join(",");
  formData.invoiceId = formData.invoiceId.join(",") || null;
  formData.invoiceAddressId = formData.invoiceAddressId.join(",") || null;
  getOrderSettlementInfo(formData)
    .then((res) => {
      finalStatement.value = res.data;
      current.value = 1;
    })
    .finally(() => {
      modalloading.value = false;
    });
};
const SettleAccountsRef = ref();
//结算回调
const statement = (data) => {
  getOrderSettlementInfo(data)
    .then((res) => {
      finalStatement.value = res.data;
    })
    .finally(() => {
      SettleAccountsRef.value.tableLoading = false;
    });
};
//返回选择商品
const pressUndo = (data) => {
  current.value = data;
};
//提交成功
const verify = () => {
  visible.value = false;
  loadPagination(true);
};
//取消订单回调
const cancel = () => {
  visible.value = false;
};
//选择商品
const UserGradeChooseCloneRef = ref();
const selectGoods = () => {
  if (form.value.bodyId && form.value.membersId) {
    visible1.value = true;
    // // UserGradeChooseCloneRef.value.loadPagination(true);
  } else {
    message.error("请选择用户和主体信息");
  }
};
loadPagination(true);
</script>
<style lang="less" scoped></style>
